/***************************************************************************//**
 * @file CGI/Aircraft.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef AIRCRAFT_CPP
#define AIRCRAFT_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <CGI/Aircraft.h>
#include <CGI/NodeCallback.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

Aircraft::Aircraft( CGI_Data *data ) :
    m_data ( data ),

    m_aircraftModel ( 0 ),
    m_aircraftForce ( 0 )
{
    m_aircraftGroup = createAircraftGroup();

    m_aircraftData = new DataTypeAircraft( m_aircraftGroup.get() );

    m_aircraftGroup->setUserData( m_aircraftData.get() );
    m_aircraftGroup->setUpdateCallback( new NodeCallback<DataTypeAircraft> );
}

////////////////////////////////////////////////////////////////////////////////

Aircraft::~Aircraft()
{
    if ( m_aircraftModel ) delete m_aircraftModel; m_aircraftModel = 0;
    if ( m_aircraftForce ) delete m_aircraftForce; m_aircraftForce = 0;
}

////////////////////////////////////////////////////////////////////////////////

void Aircraft::update()
{
    osg::Quat  attitude;
    osg::Vec3d position;

    attitude.makeRotate( m_data->dataInp.aircraft.wgs_phi, osg::Vec3d( 1.0, 0.0, 0.0 ),
                         m_data->dataInp.aircraft.wgs_tht, osg::Vec3d( 0.0, 1.0, 0.0 ),
                         m_data->dataInp.aircraft.wgs_psi, osg::Vec3d( 0.0, 0.0, 1.0 ) );

    position = osg::Vec3d( m_data->dataInp.aircraft.wgs_x,
                           m_data->dataInp.aircraft.wgs_y,
                           m_data->dataInp.aircraft.wgs_z );

    m_aircraftData->setAttitude( attitude );
    m_aircraftData->setPosition( position );

    m_aircraftModel->update();
    m_aircraftForce->update();
}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::Group> Aircraft::createAircraftGroup()
{
    osg::ref_ptr<osg::Group> aircraftGroup = new osg::Group();
    aircraftGroup->setName( "AircraftGroup" );

    osg::ref_ptr<osg::PositionAttitudeTransform> aircraftPAT = new osg::PositionAttitudeTransform();
    aircraftPAT->setName( "AircraftPAT" );

    m_aircraftModel = new AircraftModel( m_data );
    m_aircraftForce = new AircraftForce( m_data );

    aircraftPAT->addChild( m_aircraftModel->getAircraftModelGroup().get() );
    aircraftPAT->addChild( m_aircraftForce->getAircraftForceGroup().get() );

    aircraftGroup->addChild( aircraftPAT.get() );

    return aircraftGroup;
}
